const Product = require('../modules/product');
const Cart = require('../modules/cart');
const CartItem = require('../modules/cart-item');

exports.getIndex = (req, res, next) => {
    req.user.getProducts()
    .then(products => {
        res.render('shop/index', { 
            prods: products, 
            pageTitle: '商店', 
            path: '/' 
        });
    })
    .catch(err => console.log(err))
}

exports.getProductList = (req, res, next) => {
    req.user.getProducts()
    .then(products => {
        res.render('shop/product-list', { 
            prods: products, 
            path: '/product-list',
            pageTitle: '所有产品'
        });
    })
    .catch(err => console.log(err))
}

exports.getProductDetail = (req, res, next) => {
    const prodId = req.params.prodId;
    Product.findByPk(prodId)
    .then(product => {
        res.render('shop/product-detail', { 
            product: product, 
            path: '/product-list',
            pageTitle: product.title
        });
    })
    .catch(err => console.log(err))
}
exports.getCart = (req, res, next) => {
    req.user.getCart()
    .then(cart => {
        return cart.getProducts()
    })
    .then(products => {
        let totalPrice = 0;
        products.forEach(p => {
            totalPrice += p.price * p.cartItem.quantity;
        });
        res.render('shop/cart', {
            products: products,
            totalPrice,
            path: '/cart',
            pageTitle: '我的购物车'
        })
    })
    .catch(err => console.log(err))
}

exports.postCartAdd = (req, res, next) => {
    const prodId = req.body.prodId;
    let currentCart;
    let currentQuantity = 1;
    req.user.getCart()
    .then(cart => {
        currentCart = cart;
        return cart.getProducts({ where: { id: prodId } })
    })
    .then(products => {
        let product = products[0];
        if (product) {
            currentQuantity = product.cartItem.quantity + 1;
            return products;
        } else {
            return req.user.getProducts({ where: { id: prodId} });
        }
    })
    .then(products => {
        console.log('===========================');
        return currentCart.addProduct(products[0], { through: { quantity: currentQuantity } })
    })
    .then(product => {
        res.redirect('/cart');
    })
    .catch(err => console.log(err))
}

exports.postCartDelete = (req, res, next) => {
    const prodId = req.body.prodId;
    return req.user.getCart()
    .then(cart => {
        return cart.getProducts({ where: { id: prodId } })
    })
    .then(products => {
        let product = products[0];
        return product.cartItem.destroy();
    })
    .then(result => {
        res.redirect('/cart');
    })
    .catch(err => console.log(err))
}

exports.postOrder = (req, res, next) => {
    let fetchedOrder;
    let fetchCart;
    req.user.createOrder()
    .then(order => {
        console.log(order);
        fetchedOrder = order;
        return req.user.getCart({ include: 'products' })
    })
    .then(cart => {
        fetchCart = cart;
        return fetchedOrder.addProducts(cart.products.map(p => {
            p.orderItem = { quantity: p.cartItem.quantity };
            return p;
        }))
    })
    .then(result => {
        return fetchCart.setProducts(null);
    })
    .then(result => {
        res.redirect('/cart');
    })
    .catch(err => console.log(err))
}

exports.getOrder = (req, res, next) => {
    req.user.getOrders({ include: 'products' })
    .then(orders => {
        res.render('shop/order', {
            path: '/order',
            pageTitle: '我的订单',
            orders
        })
    })
    .catch(err => console.log(err))
}
